// pages/studentAdd/studentAdd.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sexShow: false,
    classifyShow: false,
    _id:'',
    name:'',
    sex:'',
    classify:'',
    remark:'',
    sexs: [
      {
        name: '男',
      },
      {
        name: '女',
      },
    ],
    classifys:[{
      name:'小学'
    },{
      name:'初中'
    },{
      name:'高中'
    }]
  },
  sexClose() {
    this.setData({ sexShow: false });
  },
  classifyClose() {
    this.setData({ classifyShow: false });
  },


  sexSelect(event) {
    console.log(event.detail);
    this.setData({
      sex:event.detail.name
    })
  },

  classifySelect(event){
    this.setData({
      classify:event.detail.name
    })
  },

  sexOpen(){
    this.setData({ sexShow: true });
  },

  classifyOpen(){
    this.setData({  classifyShow: true });
  },
  async submit(event){
    let app = getApp();
    console.log(app.globalData.students);
    let data = event.detail.value
    console.log(data);
    if(data.name==''||data.sex==''||data.classify==''){
      wx.showToast({
        title:'填写不完整',
        icon:'error'
      })
      return
    }
    wx.cloud.database().collection('groups').doc(this.data._id).update({
      data:{
        name:this.data.name,
        remark:this.data.remark,
        students:app.globalData.students,
      }
    }).then((res)=>{
      wx.showToast({
        title:'修改成功',
      });
      this.setData({
        name:'',
        reamrk:'',
      });
      wx.navigateBack()
    })
  },
  async delete(){
    wx.showModal({
      title:'提示',
      content:'确定要删除该班级吗？',
      success:async()=>{
        wx.showLoading({title:'删除中',mask:true})
        let groupId = this.data._id;
        await wx.cloud.database().collection('groups').doc(groupId).remove().then(async res=>{
          wx.showToast({
            title:'删除成功'
          });
        })
        wx.navigateBack()
      }
    })
  },
  toPage(){
    wx.navigateTo({url:`/pages/studentsEdit/studentsEdit`})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(event) {
    console.log(event);
    this.setData({
      _id:event._id
    })
    let _id = event._id;
    let app = getApp();
    console.log(app.globalData.students);
    app.globalData.groupId = _id;
    wx.cloud.database().collection('groups').doc(_id).get().then(res=>{
      console.log(res);
      this.setData({
        name:res.data.name,
        remark:res.data.remark,
        students:res.data.students,
        _id:_id
      });
      if(res.data.students){
        app.globalData.students = res.data.students
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})